package m09;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JButton;

import m07.GamePanel;


/**
 * The JFrame that shows that a random event has happened and handles the event.
 * @author kateharlan
 *
 */
public class RandomEncounterFrame extends JFrame{
	private static final long serialVersionUID = -5855860650782823610L;
	private JPanel contentPane;
	private Trader curTrader;
	private GamePanel gp;


	/**
	 * Create the frame.
	 * @param the player that's encountering the trader.
	 */
	public RandomEncounterFrame(GamePanel gp) {
		
		curTrader = new Trader("Trader");
		this.gp = gp;
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JLabel lblYouHaveRandomly = new JLabel("You have randomly encountered a trader!");
		lblYouHaveRandomly.setBounds(6, 26, 440, 16);
		lblYouHaveRandomly.setHorizontalAlignment(SwingConstants.CENTER);
		lblYouHaveRandomly.setForeground(Color.white);
		contentPane.add(lblYouHaveRandomly);
		
		JLabel lblWouldYouLike = new JLabel("Would you like to...");
		lblWouldYouLike.setHorizontalAlignment(SwingConstants.CENTER);
		lblWouldYouLike.setBounds(135, 72, 182, 16);
		lblWouldYouLike.setForeground(Color.white);
		contentPane.add(lblWouldYouLike);
		
		JButton btnFight = new JButton("Fight");
		btnFight.setBounds(37, 141, 117, 29);
		btnFight.addActionListener(new FightButton());
		contentPane.add(btnFight);
		
		JButton btnFlee = new JButton("Flee");
		btnFlee.setBounds(166, 141, 117, 29);
		btnFlee.addActionListener(new FleeButton());
		contentPane.add(btnFlee);
		
		JButton btnTrade = new JButton("Trade");
		btnTrade.setBounds(295, 141, 117, 29);
		btnTrade.addActionListener(new TradeButton());
		contentPane.add(btnTrade);
		
		BufferedImage myPicture;
		try {
			ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
			InputStream input = classLoader.getResourceAsStream("config_screen.png");
			myPicture = ImageIO.read(input);
			JLabel picLabel = new JLabel(new ImageIcon(myPicture));
			picLabel.setBounds(0, 0, 450, 300);
			contentPane.add(picLabel);
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("Background Not Loaded");
		}
	}
	
	/**
	 * a getter for this RandomEncounterFrame
	 * @return the current RandomEncounterFrame for use in the buttons.
	 */
	private RandomEncounterFrame getREFrame(){
		return RandomEncounterFrame.this;
	}
	
	/**
	 * a listener for the fight button
	 * @author kateharlan
	 *
	 */
	private class FightButton implements ActionListener {

		/**
		 * allows the player to fight the trader.
		 * the fight is based on the fighter points and randomness.
		 * if the player wins, they get money
		 * if they lose, they lose money
		 * otherwise nothing happens
		 */
		@Override
		public void actionPerformed(ActionEvent arg0) {
			curTrader.fight();
			getREFrame().setVisible(false);
			gp.updateMoney();
			gp.updatePlayer();
		}
	}
	
	/**
	 * a listener for the flee button
	 * @author kateharlan
	 *
	 */
	private class FleeButton implements ActionListener {

		/**
		 * allows the player to flee from the trader.
		 * the fight is based on the pilot points and randomness.
		 * if the player wins, they get away
		 * if they lose, they have to trade
		 * otherwise they have to fight
		 */
		@Override
		public void actionPerformed(ActionEvent arg0) {
			curTrader.flee(gp);
			getREFrame().setVisible(false);
			gp.updateMoney();
			gp.updateFuel();
			gp.updatePlayer();
		}
	}
	
	/**
	 * a listener for the trade button
	 * @author kateharlan
	 */
	private class TradeButton implements ActionListener {

		/**
		 * allows the player to trade with the trader.
		 */
		@Override
		public void actionPerformed(ActionEvent arg0) {
			getREFrame().setVisible(false);
			curTrader.trade(gp);
			gp.updateMoney();
			gp.updateFuel();
			gp.updatePlayer();
		}
	}
}
